package com.ht.springcloud.item.controller;

import com.ht.springcloud.item.pojo.ItemH;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/items")
public class ItemControllerH {
    private int count;
    @GetMapping("/item/{id}")
    @HystrixCommand(fallbackMethod = "buibui")
    public ItemH getItemById(@PathVariable Integer id, HttpServletRequest request) {
        System.err.println(request.getRemoteAddr());
        count++;
        System.err.println(count);
        ItemH item = new ItemH();
        item.setId(id);
        item.setCategory("电脑");
        item.setName("笔记本电脑");
        item.setPrice(1000000);
        return item;
    }
    @GetMapping("/item/test1")
    public String getItemById(String username,Integer age) {
        return "收到 name:>>>>>" +username+ "age>>>" +age;
    }
    @PostMapping("/item/item")
    public ItemH getItemById(@RequestBody ItemH item){
        item.setName(item.getName()+"你好啊");
        return item;
    }

    public ItemH buibui(@PathVariable Integer id, HttpServletRequest request) {
        System.err.println("降级了");

        System.err.println(count);
        ItemH item = new ItemH();
        item.setId(id+111);
        item.setCategory("苹果");
        item.setName("小苹果");
        item.setPrice(-1000000);
        return item;
    }
}
